<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;
class Order extends \app\BaseModel
{
    protected $name = "massage_service_order_list";
    protected $append = ["coach_info", "order_goods", "all_goods_num", "address_info"];

    public function getPayModelAttr($value, $data)
    {
        if (isset($value) && isset($data["balance"])) {
            if ($value == 1 && 0 < $data["balance"]) {
                $value = 2;
            }
            return $value;
        }
    }

    public function getAddressInfoAttr($value, $data)
    {
        if (!empty($data["id"])) {
            $address_model = new OrderAddress();
            $address_info = $address_model->dataInfo(["order_id" => $data["id"]]);
            return $address_info;
        }
    }

    public function dataInfo($dis, $field = "*")
    {
        $data = $this->where($dis)->field($field)->find();
        return !empty($data) ? $data->toArray() : [];
    }

    public function getAllGoodsNumAttr($value, $data)
    {
        if (!empty($data["id"])) {
            $order_goods_model = new OrderGoods();
            $dis = ["order_id" => $data["id"], "status" => 1];
            $num = $order_goods_model->where($dis)->sum("num");
            return $num;
        }
    }

    public function getOrderGoodsAttr($value, $data)
    {
        if (!empty($data["id"])) {
            $order_goods_model = new OrderGoods();
            $dis = ["order_id" => $data["id"], "status" => 1];
            $list = $order_goods_model->where($dis)->select()->toArray();
            return $list;
        }
    }

    public function getInitMaterialPriceAttr($value, $data)
    {
        if (isset($value) && isset($data["material_price"])) {
            if (0 < $value) {
                return $value;
            }
            return $data["material_price"];
        }
    }

    public function getCoachInfoAttr($value, $data)
    {
        if (isset($data["coach_id"]) && isset($data["id"]) && isset($data["add_pid"])) {
            if (!empty($data["coach_id"]) && 0 < $data["coach_id"]) {
                $info = \think\facade\Db::name("massage_service_coach_list")->where(["id" => $data["coach_id"]])->field("id,city_id,uniacid,coach_name,mobile,work_img")->find();
            } else {
                $change_log_model = new CoachChangeLog();
                $order_id = !empty($data["add_pid"]) ? $data["add_pid"] : $data["id"];
                $info["coach_name"] = $change_log_model->where(["order_id" => $order_id])->order("id desc")->value("now_coach_name");
                $info["mobile"] = $change_log_model->where(["order_id" => $order_id])->order("id desc")->value("now_coach_mobile");
                $info["work_img"] = defaultCoachAvatar();
            }
            return $info;
        }
    }

    public function indexDataList($dis, $mapor, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_order_goods_list b", "a.id = b.order_id")->where($dis)->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->field("a.pay_model,a.start_service_time,a.balance,a.label_time,a.have_tx,a.id,a.coach_id,a.store_id,a.is_comment,a.order_code,a.true_service_price,a.pay_type,a.pay_price,a.start_time,a.create_time,a.user_id,a.end_time,a.add_pid,a.is_add,a.init_material_price,a.material_price")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        if (!empty($data["data"])) {
            $abn_model = new \app\abnormalorder\model\OrderList();
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                $v["start_time"] = date("Y-m-d H:i", $v["start_time"]);
                $v["end_time"] = date("Y-m-d H:i", $v["end_time"]);
                $v["abn_order_id"] = $abn_model->where(["order_id" => $v["id"]])->value("id");
            }
        }
        return $data;
    }

    public function coachDataList($dis, $mapor, $page = 10, $sort = "a.id desc")
    {
        $data = $this->alias("a")->join("massage_service_order_goods_list b", "a.id = b.order_id")->where($dis)->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->field("a.pay_model,a.car_type,a.car_price,a.coach_cash,a.distance,a.start_service_time,a.balance,a.label_time,a.have_tx,a.id,a.coach_id,a.store_id,a.is_comment,a.order_code,a.true_service_price,a.pay_type,a.pay_price,a.start_time,a.create_time,a.user_id,a.end_time,a.add_pid,a.is_add,a.init_material_price,a.material_price")->group("a.id")->order($sort)->paginate($page)->toArray();
        if (!empty($data["data"])) {
            $abn_model = new \app\abnormalorder\model\OrderList();
            foreach ($data["data"] as &$v) {
                $v["distance"] = distance_text($v["distance"]);
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                $v["start_time"] = date("Y-m-d H:i", $v["start_time"]);
                $v["end_time"] = date("Y-m-d H:i", $v["end_time"]);
                $v["abn_order_id"] = $abn_model->where(["order_id" => $v["id"]])->value("id");
            }
        }
        return $data;
    }

    public function adminDataList($dis, $page = 10, $map = [], $phone_encryption = 0)
    {
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id", "left")->join("massage_order_coach_change_logs c", "(a.add_pid = c.order_id||a.id = c.order_id) AND c.is_new = 1", "left")->join("massage_channel_list e", "a.channel_id = e.id", "left")->where($dis)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->field("a.id,a.store_id,a.car_price,a.material_price,a.true_car_price,a.is_add,a.coach_id,a.user_id,a.distance,a.pay_price,a.type,a.car_type,a.service_price,a.true_service_price,a.start_time,a.admin_id,a.partner_id,a.order_code,a.transaction_id,a.pay_type,a.pay_model,a.balance,a.is_show,a.init_service_price,a.create_time,a.end_time,a.uniacid,a.add_pid,b.coach_name,e.user_name as channel_name,e.cate_id as channel_cate")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        if (!empty($data["data"])) {
            $user_model = new User();
            $admin_model = new Admin();
            $refund_model = new RefundOrder();
            $channel_cate = new ChannelCate();
            foreach ($data["data"] as &$v) {
                $v["admin_name"] = $admin_model->where(["id" => $v["admin_id"]])->value("agent_name");
                $v["partner_name"] = $user_model->where(["id" => $v["partner_id"]])->value("nickName");
                $v["agent_type"] = !empty($v["partner_id"]) ? 2 : 1;
                if ($phone_encryption == 0 && !empty($v["address_info"])) {
                    $v["address_info"]["mobile"] = substr_replace($v["address_info"]["mobile"], "****", 2, 4);
                }
                $v["mobile"] = !empty($v["address_info"]) ? $v["address_info"]["mobile"] : "";
                $v["user_name"] = !empty($v["address_info"]) ? $v["address_info"]["user_name"] : "";
                $v["channel"] = $channel_cate->where(["id" => $v["channel_cate"]])->value("title");
                $v["nickName"] = $user_model->where(["id" => $v["user_id"]])->value("nickName");
                $v["distance"] = distance_text($v["distance"]);
                $v["refund_price"] = $refund_model->where(["order_id" => $v["id"], "status" => 2])->sum("refund_price");
                if ($v["is_add"] == 0) {
                    $v["add_order_id"] = \think\facade\Db::name("massage_service_order_list")->where(["add_pid" => $v["id"]])->where("pay_type", ">", 1)->field("id,order_code")->select();
                } else {
                    $v["add_pid"] = \think\facade\Db::name("massage_service_order_list")->where(["id" => $v["add_pid"]])->field("id,order_code")->find();
                }
                $v["can_refund_price"] = $this->getOrderRefundPrice($v);
            }
        }
        return $data;
    }

    public function getOrderRefundPrice($order)
    {
        return round($order["true_service_price"] + $order["true_car_price"] + $order["material_price"], 2);
    }

    public function adminDataListV2($dis, $page = 10, $map = [], $phone_encryption = 0)
    {
        $data = $this->where($dis)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->field("coach_show,channel_qr_id,broker_id,free_fare,free_fare,id,discount,store_id,channel_id,car_price,material_price,true_car_price,is_add,coach_id,user_id,distance,pay_price,type,car_type,service_price,true_service_price,start_time,admin_id,partner_id,order_code,transaction_id,pay_type,pay_model,balance,is_show,init_service_price,create_time,end_time,uniacid,add_pid")->order("id desc")->paginate($page)->toArray();
        if (!empty($data["data"])) {
            $user_model = new User();
            $admin_model = new Admin();
            $refund_model = new RefundOrder();
            $channel_list = new ChannelList();
            $channel_qr_model = new ChannelQr();
            $order_data_model = new OrderData();
            $broker_model = new \app\coachbroker\model\CoachBroker();
            $abn_model = new \app\abnormalorder\model\OrderList();
            foreach ($data["data"] as &$v) {
                $v["car_price"] = round($v["car_price"], 2);
                $v["total_price"] = round($v["pay_price"] + $v["discount"], 2);
                if (!empty($v["channel_id"])) {
                    $channel_info = $channel_list->dataInfo(["id" => $v["channel_id"]]);
                    if (!empty($channel_info)) {
                        $v["channel_name"] = $channel_info["user_name"];
                        $v["channel"] = $channel_info["cate_text"];
                    }
                }
                if (!empty($v["channel_qr_id"])) {
                    $v["channel_qr_name"] = $channel_qr_model->where(["id" => $v["channel_qr_id"]])->value("title");
                } else {
                    $v["channel_qr_name"] = "";
                }
                $v["coach_name"] = !empty($v["coach_info"]) ? $v["coach_info"]["coach_name"] : "";
                if (!empty($v["admin_id"])) {
                    $v["admin_name"] = $admin_model->where(["id" => $v["admin_id"]])->value("agent_name");
                } else {
                    $v["admin_name"] = "";
                }
                if (!empty($v["broker_id"])) {
                    $v["partner_name"] = $broker_model->where(["id" => $v["broker_id"]])->value("user_name");
                } else {
                    $v["partner_name"] = "";
                }
                $v["agent_type"] = !empty($v["partner_id"]) ? 2 : 1;
                if ($phone_encryption == 0 && !empty($v["address_info"])) {
                    $v["address_info"]["mobile"] = substr_replace($v["address_info"]["mobile"], "****", 2, 4);
                }
                $v["mobile"] = !empty($v["address_info"]) ? $v["address_info"]["mobile"] : "";
                $v["user_name"] = !empty($v["address_info"]) ? $v["address_info"]["user_name"] : "";
                $v["nickName"] = $user_model->where(["id" => $v["user_id"]])->value("nickName");
                $v["distance"] = distance_text($v["distance"]);
                $v["refund_price"] = $refund_model->where(["order_id" => $v["id"], "status" => 2])->sum("refund_price");
                if ($v["is_add"] == 0) {
                    $v["add_order_id"] = \think\facade\Db::name("massage_service_order_list")->where(["add_pid" => $v["id"]])->where("pay_type", ">", 1)->field("id,order_code")->select();
                } else {
                    $v["add_pid"] = \think\facade\Db::name("massage_service_order_list")->where(["id" => $v["add_pid"]])->field("id,order_code")->find();
                }
                $v["can_refund_price"] = $this->getOrderRefundPrice($v);
                if ($v["is_add"] == 0) {
                    $v["admin_apply_refund"] = $this->orderCanRefundV2($v["order_goods"], $v["pay_type"]);
                }
                $v["add_flow_path"] = $order_data_model->where(["order_id" => $v["id"]])->value("add_flow_path");
                $v["abn_order_id"] = $abn_model->where(["order_id" => $v["id"]])->value("id");
            }
        }
        return $data;
    }

    public function orderCanRefundV2($order_goods, $pay_type)
    {
        $can_refund_num = is_array($order_goods) ? array_sum(array_column($order_goods, "can_refund_num")) : 0;
        if (in_array($pay_type, [2, 3, 4, 5, 6]) && 0 < $can_refund_num) {
            return 1;
        }
        return 0;
    }

    public function getCoachOrderId($name)
    {
        $map[] = ["b.coach_name", "like", "%" . $name . "%"];
        $map[] = ["c.now_coach_name", "like", "%" . $name . "%"];
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id", "left")->join("massage_order_coach_change_logs c", "if(a.is_add=0,a.id,a.add_pid) = c.order_id AND c.is_new = 1", "left")->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->column("a.id");
        return $data;
    }

    public function getOrderIdByChannelCate($channel_cate_id)
    {
        $channel_list = new ChannelList();
        $dis = ["cate_id" => $channel_cate_id];
        $id = $channel_list->where($dis)->column("id");
        return $id;
    }

    public function getOrderIdByChannelName($channel_cate_name)
    {
        $channel_list = new ChannelList();
        $dis[] = ["user_name", "like", "%" . $channel_cate_name . "%"];
        $id = $channel_list->where($dis)->column("id");
        return $id;
    }

    public function adminOrderPrice($dis, $map = [])
    {
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id", "left")->join("massage_order_coach_change_logs c", "if(a.is_add=0,a.id,a.add_pid) = c.order_id AND c.is_new = 1", "left")->join("massage_channel_list e", "a.channel_id = e.id", "left")->where($dis)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->group("a.id")->column("a.id");
        $arr["order_price"] = $this->where("id", "in", $data)->sum("true_service_price");
        $arr["car_price"] = $this->where("id", "in", $data)->sum("true_car_price");
        $arr["order_price"] = round($arr["order_price"], 2);
        $arr["car_price"] = round($arr["car_price"], 2);
        return $arr;
    }

    public function adminOrderPriceV2($dis, $map = [])
    {
        $arr["car_price"] = $this->where($dis)->where(["free_fare" => 0])->where("pay_time", "<>", 0)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->sum("car_price");
        $arr["pay_price"] = $this->where($dis)->where("pay_time", "<>", 0)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->sum("pay_price");
        $arr["service_price"] = $this->where($dis)->where("pay_time", "<>", 0)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->sum("service_price");
        $arr["material_price"] = $this->where($dis)->where("pay_time", "<>", 0)->where("start_material_price", ">", 0)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->sum("start_material_price");
        foreach ($dis as $k => $v) {
            $dis[$k][0] = "a." . $v[0];
        }
        if (!empty($map)) {
            foreach ($map as $k => $v) {
                $dis[$k][0] = "a." . $v[0];
            }
        }
        $list = ["refund_service_price", "refund_car_price", "refund_material_price", "refund_price"];
        foreach ($list as $value) {
            $arr[$value] = 0;
        }
        $refund_cash = $this->alias("a")->join("massage_service_refund_order b", "a.id = b.order_id")->where($dis)->where("a.pay_time", "<>", 0)->where("b.status", "=", 2)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->field("sum(b.refund_material_price) as refund_material_price,sum(refund_service_price) as refund_service_price,sum(refund_car_price) as refund_car_price,sum(refund_price) as refund_price")->find();
        if (!empty($refund_cash)) {
            $refund_cash = $refund_cash->toArray();
            foreach ($refund_cash as $k => $v) {
                $arr[$k] = $v;
            }
        }
        foreach ($arr as $k => $v) {
            $arr[$k] = round($v, 2);
        }
        return $arr;
    }

    public function adminDataSelect($dis, $map = [], $phone_encryption = 0, $is_channel = 0)
    {
        $data = $this->alias("a")->join("massage_service_coach_list b", "a.coach_id = b.id")->join("massage_service_order_goods_list c", "a.id = c.order_id")->join("massage_service_order_address h", "a.id = h.order_id")->join("massage_channel_list e", "a.channel_id = e.id", "left")->join("shequshop_school_admin f", "a.admin_id = f.id", "left")->join("massage_channel_qr g", "a.channel_qr_id = g.id", "left")->join("massage_order_coach_change_logs d", "(a.add_pid = d.order_id||a.id = d.order_id) AND d.is_new = 1", "left")->where($dis)->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->field("a.coach_id,a.store_id,a.start_time,a.create_time,a.pay_model,a.balance,a.pay_type,a.is_add,a.car_price,a.init_service_price,a.pay_price,a.order_code,a.transaction_id,a.id as order_id,b.coach_name,h.user_name,h.mobile,c.goods_name,g.title as channel_qr_name,a.channel_qr_id,f.agent_name as admin_name,a.is_add,b.coach_name,c.goods_id,c.goods_name,c.price,c.num,e.user_name as channel_name,e.cate_id as channel_cate,c.init_material_price as init_material_prices")->group("a.id,c.goods_id")->order("a.id desc")->select()->toArray();
        if (!empty($data)) {
            $channel_cate = new ChannelCate();
            $refund_model = new RefundOrder();
            foreach ($data as &$v) {
                $v["id"] = $v["order_id"];
                $v["init_material_price"] = $v["init_material_prices"];
                if ($is_channel == 1) {
                    $v["channel"] = $channel_cate->where(["id" => $v["channel_cate"]])->value("title");
                }
                if ($phone_encryption == 0) {
                    $v["mobile"] = substr_replace($v["mobile"], "****", 2, 4);
                }
                $v["refund_price"] = $refund_model->where(["order_id" => $v["id"], "status" => 2])->sum("refund_price");
            }
        }
        return $data;
    }

    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }

    public function datePrice($date, $uniacid, $cap_id = 0, $end_time = "", $type = 1)
    {
        $end_time = !empty($end_time) ? $end_time : $date + 86399;
        $dis = [];
        $dis[] = ["transaction_id", "<>", ""];
        $dis[] = ["auto_refund", "=", 0];
        $dis[] = ["create_time", "between", $date . "," . $end_time];
        $dis[] = ["uniacid", "=", $uniacid];
        if (!empty($cap_id)) {
            $dis[] = ["cap_id", "=", $cap_id];
        }
        if ($type == 1) {
            $price = $this->where($dis)->sum("pay_price");
            return round($price, 2);
        }
        $count = $this->where($dis)->count();
        return $count;
    }

    public function payOrderInfo($user_id, $cap_id, $lat = 0, $lng = 0, $car_type = 0, $coupon = 0, $order_id = 0, $service_type = 1, $order_start_time = 0, $input = [])
    {
        $is_remember = isset($input["is_remember"]) ? $input["is_remember"] : 1;
        $pay_model = isset($input["pay_model"]) ? $input["pay_model"] : 4;
        if (0 < $coupon) {
            $is_remember = 0;
        }
        $car_model = new Car();
        $coupon_model = new Coupon();
        $coach_model = new Coach();
        $car_config_model = new CarPrice();
        $admin_config_model = new AdminConfig();
        $order_server = new \app\massage\server\Order();
        $coach = $coach_model->dataInfo(["id" => $cap_id]);
        $car_list = $car_model->carPriceAndCount($user_id, $cap_id, 0, $order_id, $service_type);
        if ($is_remember == 0) {
            $car_list = $coupon_model->orderCouponData($car_list, $coupon);
        }
        $goods_price = $car_list["car_price"];
        $data["coupon_id"] = $car_list["coupon_id"];
        $data["init_material_price"] = $car_list["all_material_price"];
        $data["material_price"] = round($data["init_material_price"] - $car_list["total_material_discount"], 2);
        $data["order_goods"] = $car_list["list"];
        $data["discount"] = $car_list["total_discount"];
        $data["init_goods_price"] = round($goods_price, 2);
        $data["goods_price"] = round($goods_price - $car_list["total_goods_discount"], 2);
        $data["free_fare"] = 0;
        $data = $order_server->memberDiscountData($data, $user_id, $is_remember);
        if (0 < $coupon || !empty($data["is_remember"])) {
            $pay_model = 0;
        }
        if (!empty($cap_id) && $service_type == 1) {
            $data["car_config"] = $car_config_model->getCityConfig($coach["uniacid"], $coach["city_id"], $order_start_time);
            if ($lat == 0) {
                $data["car_price"] = 0;
                $data["distance"] = 0;
            } else {
                $data["distance"] = getDriveDistance($coach["lng"], $coach["lat"], $lng, $lat, $coach["uniacid"]);
                $data["distance"] += $data["car_config"]["invented_distance"] * $data["distance"] / 100;
                $data["car_price"] = $this->getCarPrice($data["distance"], $data["car_config"], $car_type);
                if (!empty($coach["admin_id"])) {
                    $admin_config = $admin_config_model->dataInfo(["admin_id" => $coach["admin_id"], "uniacid" => $coach["uniacid"]]);
                    $free_fare = $admin_config["free_fare_bear"] != 1 ? $admin_config["free_fare_bear"] : 3;
                } else {
                    $free_fare = getConfigSetting($coach["uniacid"], "free_fare_bear");
                }
                if (!empty($coach["free_fare_distance"]) && 0 < $coach["free_fare_distance"] && 0 < $free_fare && $data["distance"] <= $coach["free_fare_distance"] * 1000) {
                    $data["free_fare"] = $free_fare;
                }
            }
        } else {
            $data["car_price"] = 0;
            $data["distance"] = 0;
        }
        $data["coach_info"] = $coach;
        $car_price = $data["free_fare"] == 0 ? $data["car_price"] : 0;
        $balance_discount_card_arr = !empty($input["balance_discount_card_arr"]) ? explode(",", $input["balance_discount_card_arr"]) : [];
        $data = $order_server->balanceDiscountData($data, $user_id, $data["goods_price"] + $data["material_price"], $car_price, $pay_model, 666, $balance_discount_card_arr);
        $data["pay_price"] = round($data["goods_price"] + $car_price + $data["material_price"], 2);
        $data["coach_id"] = $cap_id;
        $data["store_id"] = !empty($coach["store_id"]) ? $coach["store_id"] : 0;
        $service_model = new Service();
        $data["order_goods"] = $service_model->giveListMemberInfo($data["order_goods"], 666, $user_id, 2);
        return $data;
    }

    public function getCarPrice($distance, $config, $car_type = 1)
    {
        if ($car_type == 0) {
            return 0;
        }
        if (!empty($config["day_type"]) && $config["day_type"] == 2) {
            $config["distance_free"] = $config["distance_free_night"];
            $config["start_distance"] = $config["start_distance_night"];
            $config["start_price"] = $config["start_price_night"];
            $config["distance_price"] = $config["distance_price_night"];
            $config["cash_type"] = $config["cash_type_night"];
            $config["cash_setting_day"] = $config["cash_setting_night"];
        }
        $start = $config["start_distance"];
        $start_price = $config["start_price"];
        $to_price = $config["distance_price"];
        $distance = $distance / 1000;
        if ($start < $distance) {
            if ($config["cash_type"] == 2 && !empty($config["cash_setting_day"])) {
                $to_price = $this->stepCarPrice($start, $config, $distance);
            } else {
                $to_price = round($distance - $start, 2) * $to_price;
            }
            $total = $start_price + $to_price;
            return round($total * 2, 2);
        }
        return round($start_price * 2, 2);
    }

    public function stepCarPrice($start, $config, $distance)
    {
        $to_price = 0;
        $have_km = $start;
        foreach ($config["cash_setting_day"] as $k => $v) {
            if ($distance <= $have_km) {
                return $to_price;
            }
            $true_km = $distance < $v["km"] || count($config["cash_setting_day"]) == $k + 1 ? $distance : $v["km"];
            $km = round($true_km - $have_km, 2);
            $cash = $v["cash"] * $km;
            $to_price += $cash;
            $have_km = $true_km;
        }
        return $to_price;
    }

    public function storeOrDoor($user_id, $cap_id, $uniacid)
    {
        $car_model = new Car();
        $car_list = $car_model->carList($user_id, $cap_id, 0, 0, 0);
        $is_store = array_column($car_list, "is_store");
        $data["is_store"] = in_array(1, $is_store) ? 1 : 0;
        $is_door = array_column($car_list, "is_door");
        $data["is_door"] = in_array(1, $is_door) ? 1 : 0;
        $coach_model = new Coach();
        $coach = $coach_model->dataInfo(["id" => $cap_id]);
        if (!empty($coach["store_id"])) {
            $store_model = new \app\store\model\StoreList();
            $store_info = $store_model->where(["id" => $coach["store_id"], "status" => 1])->field("id,title,address,lng,lat,phone")->find();
        }
        $data["is_store"] = empty($store_info) ? 0 : $data["is_store"];
        if ($data["is_store"] == 1) {
            $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$uniacid, ["store"]);
            if (empty($auth) || !$auth["store"]) {
                $data["is_store"] = 0;
            }
        }
        return $data;
    }

    public function agentCashData($admin, $order)
    {
        if (!empty($admin) && !empty($admin["admin_pid"]) && in_array($admin["city_type"], [1, 2])) {
            $order["level_balance"] = $admin["level_balance"];
            $order["admin_pid"] = $admin["admin_pid"];
            $admin_pdata = $this->dataInfo(["id" => $order["admin_pid"]]);
            if (!empty($admin_pdata) && $admin_pdata["city_type"] == 1) {
                $order["p_level_balance"] = $admin_pdata["level_balance"];
                $order["p_admin_pid"] = $admin_pdata["admin_pid"];
            }
        }
        return $order;
    }

    public function getCashData($order, $type = 1, $admin_id = 0)
    {
        $arr_data = $this->getCashDataCommon($order, $type, $admin_id);
        return $arr_data;
    }

    public function getCashDataCommon($order, $type = 1, $admin_id = 0)
    {
        $comm_model = new Commission();
        if ($type == 1) {
            $order = $comm_model->balanceData($order, $admin_id);
            if (!empty($order["code"]) && $order["code"] == 300) {
                return $order;
            }
        }
        $order = $comm_model->cashData($order, $type);
        if (empty($order["coach_id"]) && !empty($order["admin_id"])) {
            $order["admin_cash"] = $order["admin_cash"] + $order["coach_cash"] + $order["true_car_price"];
        }
        if ($type == 1) {
            $comm_model->carCommission($order);
            $comm_model->coachCommission($order);
            $comm_model->commissionAddDataV2($order);
            $comm_model->commissionLevelAddData($order);
            $comm_model->adminCommission($order);
            $comm_model->adminLevelCommission($order);
            $comm_model->adminProvinceCommission($order);
            $comm_model->partnerCommission($order);
            $comm_model->channelCommission($order);
            $comm_model->salesmanCommission($order);
            $comm_model->companyCommission($order);
            $comm_model->adminCarCommission($order);
        }
        if ($type == 3) {
            $comm_id = $comm_model->where(["order_id" => $order["id"]])->where("type", "in", [1, 14])->column("id");
            $comm_model->where(["order_id" => $order["id"]])->where("type", "in", [1, 14])->delete();
            $share_model = new CommShare();
            $share_model->where(["order_id" => $order["id"]])->where("comm_id", "in", $comm_id)->delete();
            $comm_model->commissionAddDataV2($order);
            $comm_model->commissionLevelAddData($order);
            $comm_model->where(["order_id" => $order["id"], "status" => -1])->where("type", "in", [1, 14])->update(["status" => 1]);
        }
        $arr = ["free_fare", "coach_balance", "admin_balance", "admin_id", "user_cash", "company_cash", "coach_cash", "admin_cash", "partner_id", "salesman_id", "true_car_price", "broker_id", "channel_id", "channel_qr_id"];
        foreach ($arr as $value) {
            if (key_exists($value, $order)) {
                $list[$value] = $order[$value];
            }
        }
        $arr_data["order_data"] = $list;
        $arr_data["data"] = $order;
        return $arr_data;
    }

    public function getCashDataCustom($order, $type = 1, $admin_id = 0)
    {
        $comm_model = new CommissionCustom();
        if ($type == 1) {
            $order = $comm_model->balanceDataCustom($order, $admin_id);
            if (!empty($order["code"]) && $order["code"] == 300) {
                return $order;
            }
        }
        $order = $comm_model->cashDataCustom($order);
        if (empty($order["coach_id"]) && !empty($order["admin_id"])) {
            $order["admin_cash"] = $order["admin_cash"] + $order["coach_cash"] + $order["true_car_price"];
        }
        if ($type == 1) {
            $comm_model->carCommission($order);
            $comm_model->coachCommission($order);
            $comm_model->commissionAddDataV3($order);
            $comm_model->adminLevelCommission($order);
            $comm_model->adminCommission($order);
            $comm_model->channelCommission($order);
            $comm_model->companyCommission($order);
        }
        $arr = ["coach_balance", "admin_balance", "admin_id", "user_cash", "company_cash", "coach_cash", "admin_cash", "partner_id"];
        foreach ($arr as $value) {
            if (key_exists($value, $order)) {
                $list[$value] = $order[$value];
            }
        }
        $arr_data["order_data"] = $list;
        $arr_data["data"] = $order;
        return $arr_data;
    }

    public function orderResult($order_code, $transaction_id)
    {
        $order = $this->dataInfo(["order_code" => $order_code, "transaction_id" => ""]);
        if (!empty($order) && !empty($transaction_id)) {
            \think\facade\Db::startTrans();
            $update = ["transaction_id" => $transaction_id, "pay_type" => 2, "pay_time" => time()];
            $res = $this->dataUpdate(["id" => $order["id"], "transaction_id" => ""], $update);
            if ($res == 0) {
                \think\facade\Db::rollback();
                return false;
            }
            if (0 < $order["balance"]) {
                $water_model = new BalanceWater();
                $res = $water_model->updateUserBalance($order, 2);
                if ($res == 0) {
                    \think\facade\Db::rollback();
                    return false;
                }
            }
            if ($order["pay_model"] == 4) {
                $discount_card_model = new \app\balancediscount\model\UserCard();
                $res = $discount_card_model->updateCardCash($order["id"], $order["user_id"], 1);
                if ($res == 0) {
                    \think\facade\Db::rollback();
                    return false;
                }
            }
            $order["transaction_id"] = $transaction_id;
            $comm_model = new Commission();
            $comm_model->dataUpdate(["order_id" => $order["id"], "status" => -1], ["status" => 1]);
            $order_model = new Order();
            $company_cash = $order_model->companySurplusCash($order["id"]);
            $comm_model->dataUpdate(["type" => 16, "order_id" => $order["id"]], ["company_cash" => $company_cash]);
            $order_price_log = new OrderPrice();
            $order_price_log->logAdd($order, $order["id"], 1, $order["pay_model"]);
            \think\facade\Db::commit();
            $log_model = new OrderLog();
            $log_model->addLog($order["id"], $order["uniacid"], 2, 1, 3, $order["user_id"]);
            $coach_model = new Coach();
            $coach_model->companyOrderResult($order);
            $notice_model = new NoticeList();
            $notice_model->dataAdd($order["uniacid"], $order["id"], 1, $order["admin_id"]);
            $coach_model->paySendMsg($order);
            if ($order["is_add"] == 0) {
                $call_model = new \app\reminder\model\Config();
                $call_model->sendCalled($order);
            }
            $print_model = new Printer();
            $print_model->printer($order["id"], 0);
        }
        return true;
    }

    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }

    public function companySurplusCash($id)
    {
        $order_cash = $this->where(["id" => $id])->where("transaction_id", "<>", 0)->sum("pay_price");
        $comm_model = new Commission();
        $refund_model = new RefundOrder();
        $share_model = new CommShare();
        $comm_cash = $comm_model->where(["order_id" => $id])->where("status", ">", -1)->where("type", "in", [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 13, 14, 17, 18, 19, 20, 21, 22])->sum("cash");
        $refund_cash = $refund_model->where(["order_id" => $id, "status" => 2])->sum("refund_price");
        $share_cash = $share_model->orderShareData($id);
        $admin_share_cash = $comm_model->where(["order_id" => $id, "type" => 23])->where("status", ">", -1)->sum("cash");
        $cash = round($order_cash - $comm_cash - $refund_cash - $share_cash + $admin_share_cash, 2);
        return $cash;
    }

    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }

    public function capFrozenPrice($cap_id, $total = 0, $toDay = 0)
    {
        $dis[] = ["cap_id", "=", $cap_id];
        if ($total == 0) {
            $dis[] = ["have_tx", "=", 0];
        }
        $dis[] = ["pay_type", ">", 1];
        if ($toDay == 1) {
            $price = $this->where($dis)->whereDay("create_time")->sum("cap_price");
        } else {
            $price = $this->where($dis)->sum("cap_price");
        }
        return round($price, 2);
    }

    public function capFrozenCount($cap_id, $total = 0, $toDay = 0)
    {
        $dis[] = ["cap_id", "=", $cap_id];
        if ($total == 0) {
            $dis[] = ["have_tx", "=", 0];
        }
        $dis[] = ["pay_type", ">", 1];
        if ($toDay == 1) {
            $price = $this->where($dis)->whereDay("create_time")->count();
        } else {
            $price = $this->where($dis)->count();
        }
        return $price;
    }

    public function autoCancelOrder($uniacid, $user_id = 0)
    {
        $log_model = new OrderLog();
        $dis[] = ["uniacid", "=", $uniacid];
        $dis[] = ["pay_type", "=", 1];
        $dis[] = ["over_time", "<", time()];
        if (!empty($user_id)) {
            $dis[] = ["user_id", "=", $user_id];
        }
        $time_key = "order_key_massage_time";
        if (getCache($time_key, $uniacid)) {
            return false;
        }
        setCache($time_key, 1, 10, $uniacid);
        $key = "order_key_massage";
        incCache($key, 1, $uniacid, 60);
        $key_value = getCache($key, $uniacid);
        if ($key_value == 1) {
            $arr = [new Order(), new UpOrderList()];
            foreach ($arr as $k => $values) {
                $order = $values->where($dis)->limit(5)->select()->toArray();
                if (!empty($order)) {
                    foreach ($order as $value) {
                        $this->cancelOrder($value, $k);
                        $log_model->addLog($value["id"], $value["uniacid"], -1, $value["pay_type"], 1);
                    }
                }
            }
        }
        decCache($key, 1, $uniacid);
        $order = $this->where(["pay_type" => 7])->whereColumn("can_tx_date", "<>", "order_end_time")->field("id,order_end_time,can_tx_date")->limit(50)->select()->toArray();
        if (!empty($order)) {
            foreach ($order as $value) {
                $this->dataUpdate(["id" => $value["id"]], ["can_tx_date" => $value["order_end_time"]]);
            }
        }
        return true;
    }

    public function cancelOrder($order, $is_up = 0, $is_cancel = 0)
    {
        \think\facade\Db::startTrans();
        $old_paytype = !empty($is_cancel) ? $is_cancel : $order["pay_type"];
        if ($is_up == 0) {
            $res = $this->where(["id" => $order["id"]])->where("pay_type", "=", $old_paytype)->update(["pay_type" => -1]);
        } else {
            $order_model = new UpOrderList();
            $res = $order_model->where(["id" => $order["id"]])->where("pay_type", "=", $old_paytype)->update(["pay_type" => -1]);
        }
        if ($res != 1) {
            \think\facade\Db::rollback();
            return ["code" => 500, "msg" => "取消失败"];
        }
        $goods_model = new Service();
        foreach ($order["order_goods"] as $v) {
            $res = $goods_model->setOrDelStock($v["goods_id"], $v["num"], 1);
            if (!empty($res["code"])) {
                \think\facade\Db::rollback();
                return $res;
            }
        }
        if ($is_up == 0 && ($order["pay_type"] < 3 || $order["pay_type"] == 8)) {
            $coupon_model = new CouponRecord();
            $coupon_model->couponRefund($order["id"]);
        }
        if ($is_up == 0) {
            $comm_model = new Commission();
            $comm_model->dataUpdate(["order_id" => $order["id"]], ["status" => -1]);
            $company_cash = $this->companySurplusCash($order["id"]);
            $comm_model->dataUpdate(["type" => 16, "order_id" => $order["id"]], ["company_cash" => $company_cash]);
            $res = $comm_model->refundCompanySuccess($order["id"]);
            if ($res == 0) {
                \think\facade\Db::rollback();
                return ["code" => 500, "msg" => "取消失败"];
            }
            $this->updateAddOrderTime($order, $order["time_long"] * 60);
        }
        \think\facade\Db::commit();
        return true;
    }

    public function updateAddOrderTime($pay_order, $time_long)
    {
        if (!empty($pay_order["is_add"])) {
            $list = $this->where(["add_pid" => $pay_order["add_pid"]])->where("pay_type", ">", 0)->where("start_time", ">", $pay_order["start_time"])->select()->toArray();
            if (!empty($list)) {
                foreach ($list as $value) {
                    $this->dataUpdate(["id" => $value["id"]], ["start_time" => $value["start_time"] - $time_long, "end_time" => $value["end_time"] - $time_long]);
                }
            }
        }
        return true;
    }

    public function checkTime($order, $start_time, $order_id = 0, $p_order_id = 0, $store_id = 0)
    {
        $service_model = new Service();
        $coach_model = new Coach();
        $order_model = new Order();
        $total_long = 0;
        $config_model = new Config();
        if (empty($order["order_goods"])) {
            return ["code" => 500, "msg" => "请选择服务项目"];
        }
        foreach ($order["order_goods"] as $v) {
            if (isset($v["can_refund_num"]) && $v["can_refund_num"] == 0) {
                $time_long = 0;
            } else {
                $time_long = $service_model->where(["id" => $v["service_id"]])->value("time_long");
            }
            $total_long += $time_long * $v["num"];
        }
        $end_time = $start_time + $total_long * 60;
        if (!empty($order["coach_id"])) {
            $coach = $coach_model->dataInfo(["id" => $order["coach_id"]]);
            $config = $config_model->dataInfo(["uniacid" => $coach["uniacid"]]);
            $res = $this->checkCoachTime($coach, $start_time, $end_time);
            if (!empty($res["code"])) {
                return $res;
            }
            $where[] = ["coach_id", "=", $order["coach_id"]];
            $where[] = ["pay_type", "not in", [-1, 7]];
            $where[] = ["end_time", ">", time()];
            if (!empty($p_order_id)) {
                $where[] = ["id", "<>", $p_order_id];
                $where[] = ["add_pid", "<>", $p_order_id];
            }
            if (!empty($order_id)) {
                $where[] = ["add_pid", "<>", $order_id];
                $where[] = ["id", "<>", $order_id];
            }
            $refund_model = new RefundOrder();
            $refund_ing_order = $refund_model->where("status", "in", [4, 5])->where(["refund_end" => 1])->column("order_id");
            if (!empty($refund_ing_order)) {
                $where[] = ["id", "not in", $refund_ing_order];
            }
            $order_list = $order_model->where($where)->field("id,start_time,end_time,order_end_time,pay_type,coach_id")->select()->toArray();
            $res = $this->checkOrderTime($order_list, $config, $start_time, $end_time);
            if (!empty($res["code"])) {
                return $res;
            }
            $rest_arr = $coach_model->getCoachRestTime($coach, $start_time, $end_time, $config);
            $res = $this->checkCoachRestTime($rest_arr, $start_time, $end_time);
            if (!empty($res["code"])) {
                return $res;
            }
        }
        if (!empty($store_id)) {
            $store_model = new \app\store\model\StoreList();
            $res = $store_model->checkStoreStatus($store_id, $start_time, $end_time);
            if (!empty($res["code"])) {
                return $res;
            }
        }
        $arr = ["end_time" => $end_time, "time_long" => $total_long];
        return $arr;
    }

    public function checkCoachTime($coach, $start_time, $end_time)
    {
        $all_day = 1;
        if ($coach["start_time"] != $coach["end_time"]) {
            $all_day = 0;
        }
        if (strtotime($coach["end_time"]) - strtotime($coach["start_time"]) == 86400) {
            $all_day = 1;
        }
        if ($all_day == 0) {
            $coach_start_time = strtotime($coach["start_time"]) - strtotime(date("Y-m-d", time())) + strtotime(date("Y-m-d", $start_time));
            $coach_end_time = strtotime($coach["end_time"]) - strtotime(date("Y-m-d", time())) + strtotime(date("Y-m-d", $start_time));
            if ($end_time < $coach_start_time) {
                $coach_start_time -= 86400;
                $coach_end_time -= 86400;
            }
            $coach_end_time = $coach_start_time < $coach_end_time ? $coach_end_time : $coach_end_time + 86400;
            if ($start_time < $coach_start_time || $coach_end_time < $end_time) {
                return ["code" => 500, "msg" => "不在服务时间内,服务时间:" . $coach["start_time"] . "-" . $coach["end_time"]];
            }
        }
        return true;
    }

    public function checkOrderTime($order_list, $config, $start_time, $end_time, $coach_id = 0)
    {
        $order_model = new Order();
        if (!empty($order_list)) {
            foreach ($order_list as $value) {
                if (!empty($coach_id) && $coach_id != $value["coach_id"]) {
                } else {
                    $value["end_time"] = !empty($order_end_time) && $value["end_time"] < $order_end_time ? $order_end_time : $value["end_time"];
                    $time_interval = 0 < $config["time_interval"] ? $config["time_interval"] * 60 - 1 : 0;
                    $res = is_time_crossV2($start_time, $end_time, $value["start_time"] - $time_interval, $value["end_time"] + $time_interval);
                    if (!$res) {
                        return ["code" => 500, "msg" => "该时间段已经被预约:" . date("Y-m-d H:i", $value["start_time"]) . "-" . date("Y-m-d H:i", $value["end_time"]) . "-" . $value["id"]];
                    }
                }
            }
        }
        return true;
    }

    public function checkCoachRestTime($rest_arr, $start_time, $end_time)
    {
        if (!empty($rest_arr)) {
            foreach ($rest_arr as $values) {
                $res = is_time_cross($start_time, $end_time, $values["time_str"], $values["time_str_end"]);
                if (!$res && $values["is_click"] == 1) {
                    return ["code" => 500, "msg" => "该时间段正在休息"];
                }
            }
        }
        return true;
    }

    public function coachOrdertext($input, $is_admin = 0)
    {
        $update["pay_type"] = $input["type"];
        switch ($input["type"]) {
            case 3:
                $update["receiving_time"] = time();
                break;
            case 4:
                $update["serout_time"] = time();
                $update["serout_lng"] = !empty($input["serout_lng"]) ? $input["serout_lng"] : 0;
                $update["serout_lat"] = !empty($input["serout_lat"]) ? $input["serout_lat"] : 0;
                $update["serout_address"] = !empty($input["serout_address"]) ? $input["serout_address"] : "";
                break;
            case 5:
                $update["arrive_time"] = time();
                $update["arrive_img"] = !empty($input["arrive_img"]) ? $input["arrive_img"] : "";
                $update["arr_lng"] = !empty($input["arr_lng"]) ? $input["arr_lng"] : 0;
                $update["arr_lat"] = !empty($input["arr_lat"]) ? $input["arr_lat"] : 0;
                $update["arr_address"] = !empty($input["arr_address"]) ? $input["arr_address"] : "";
                break;
            case 6:
                $update["start_service_time"] = time();
                break;
            case 7:
                $update["order_end_time"] = time();
                $update["end_lng"] = !empty($input["end_lng"]) ? $input["end_lng"] : 0;
                $update["end_lat"] = !empty($input["end_lat"]) ? $input["end_lat"] : 0;
                $update["end_address"] = !empty($input["end_address"]) ? $input["end_address"] : "";
                $update["end_img"] = !empty($input["end_img"]) ? $input["end_img"] : "";
                break;
            case -1:
                $update["coach_refund_time"] = time();
                $update["version"] = 2;
                $update["coach_refund_text"] = !empty($input["coach_refund_text"]) ? $input["coach_refund_text"] : "";
                break;
//            default:
        }
        return $update;
    }

    public function channelData($channel_id, $input = [])
    {
        $dis[] = ["a.channel_id", "=", $channel_id];
        $dis[] = ["a.pay_type", ">", 1];
        if (!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["a.create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        if (isset($input["qr_id"])) {
            $dis[] = ["a.channel_qr_id", "=", $input["qr_id"]];
        }
        $where = [];
        if (!empty($input["name"])) {
            $where[] = ["b.goods_name", "like", "%" . $input["name"] . "%"];
            $where[] = ["a.order_code", "like", "%" . $input["name"] . "%"];
        }
        $id = $this->alias("a")->join("massage_service_order_goods_list b", "a.id = b.order_id")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->group("a.id")->column("a.id");
        $service_price = $this->where("id", "in", $id)->sum("true_service_price");
        $data["order_price"] = round($service_price, 2);
        $data["order_count"] = count($id);
        $comm_model = new Commission();
        $data["all_cash"] = $comm_model->where("order_id", "in", $id)->where("status", ">", -1)->where(["type" => 10])->sum("cash");
        $data["all_cash"] = round($data["all_cash"], 2);
        return $data;
    }

    public function getOrderNum($where)
    {
        return $this->where($where)->count();
    }

    public function carMoneyList($dis, $page = 10)
    {
        $data = $this->where($dis)->field("id,coach_id,serout_time,arrive_time,car_price,order_code,trip_start_address,trip_end_address")->group("id")->order("id desc")->paginate($page)->each(function ($item) {
            $item["serout_time"] = date("Y-m-d H:i:s", $item["serout_time"]);
            $item["arrive_time"] = !empty($item["arrive_time"]) ? date("Y-m-d H:i:s", $item["arrive_time"]) : "";
            return $item;
        })->toArray();
        $address_model = new OrderAddress();
        $coach_model = new Coach();
        if (!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                if (empty($v["trip_end_address"])) {
                    $address = $address_model->dataInfo(["order_id" => $v["id"]]);
                    $v["trip_end_address"] = !empty($address) ? $address["address"] . " " . $address["address_info"] : "";
                }
                if (empty($v["trip_start_address"])) {
                    $v["trip_start_address"] = $coach_model->where(["id" => $v["coach_id"]])->value("address");
                }
            }
        }
        return $data;
    }

    public function carMoneyListV2($dis, $page = 10)
    {
        $comm_model = new Commission();
        $data = $comm_model->alias("a")->join("massage_service_order_list b", "a.order_id = b.id", "right")->where($dis)->field("a.order_id as id,b.coach_id,b.serout_time,b.arrive_time,a.cash as car_price,b.order_code,b.trip_start_address,b.trip_end_address")->group("b.id")->order("b.id desc")->paginate($page)->toArray();
        $address_model = new OrderAddress();
        $coach_model = new Coach();
        if (!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["serout_time"] = date("Y-m-d H:i:s", $v["serout_time"]);
                $v["arrive_time"] = !empty($v["arrive_time"]) ? date("Y-m-d H:i:s", $v["arrive_time"]) : "";
                if (empty($v["trip_end_address"])) {
                    $address = $address_model->dataInfo(["order_id" => $v["id"]]);
                    $v["trip_end_address"] = !empty($address) ? $address["address"] . " " . $address["address_info"] : "";
                }
                if (empty($v["trip_start_address"])) {
                    $v["trip_start_address"] = $coach_model->where(["id" => $v["coach_id"]])->value("address");
                }
            }
        }
        return $data;
    }

    public function addOrderTime($order_id)
    {
        $end_time = $this->where(["add_pid" => $order_id])->where("pay_type", ">", 0)->field("id,start_time,end_time")->max("end_time");
        if (empty($end_time)) {
            $end_time = $this->where(["id" => $order_id])->value("end_time");
        }
        return $end_time;
    }

    public function addOrderTimes($order_id)
    {
        $times = $this->where(["add_pid" => $order_id])->where("pay_type", ">", 0)->count();
        return !empty($times) ? $times : 1;
    }

    public function getOrderEndTime($order_goods, $start_time)
    {
        $total_long = 0;
        $service_model = new Service();
        foreach ($order_goods as $v) {
            $time_long = $service_model->where(["id" => $v["service_id"]])->value("time_long");
            $total_long += $time_long * $v["num"];
        }
        $end_time = $start_time + $total_long * 60;
        return $end_time;
    }

    public function orderCanAdd($order)
    {
        return in_array($order["pay_type"], [6]) && $order["is_add"] == 0 && !empty($order["address_info"]) ? 1 : 0;
    }

    public function coachCashList($dis, $page = 10, $month = "")
    {
        if (!empty($month)) {
            $firstday = date("Y-m-01", $month);
            $lastday = date("Y-m-d H:i:s", strtotime($firstday . " +1 month") - 1);
            $data = $this->where($dis)->whereTime("create_time", "<=", $lastday)->field("id,coach_id,order_code,pay_type,pay_price,start_time,create_time,user_id,end_time,add_pid,is_add,coach_cash,true_car_price")->order("create_time desc,id desc")->paginate($page)->toArray();
        } else {
            $data = $this->where($dis)->order("create_time desc,id desc")->field("id,coach_id,order_code,pay_type,pay_price,start_time,create_time,user_id,end_time,add_pid,is_add,coach_cash,true_car_price")->paginate($page)->toArray();
        }
        return $data;
    }

    public function adminCashList($dis, $page = 10, $month = "")
    {
        if (!empty($month)) {
            $firstday = date("Y-m-01", $month);
            $lastday = date("Y-m-d H:i:s", strtotime($firstday . " +1 month") - 1);
            $data = $this->where($dis)->whereTime("create_time", "<=", $lastday)->field("id,create_time,can_tx_date,add_pid,end_time,pay_type,have_tx,uniacid,user_id,coach_id,pay_price,true_service_price,true_car_price,start_time,coach_cash")->order("create_time desc,id desc")->paginate($page)->toArray();
        } else {
            $data = $this->where($dis)->order("create_time desc,id desc")->field("id,create_time,can_tx_date,add_pid,end_time,pay_type,have_tx,uniacid,user_id,coach_id,pay_price,true_service_price,true_car_price,start_time,coach_cash")->paginate($page)->toArray();
        }
        return $data;
    }

    public function getFinanceOrderId($name, $type = 0)
    {
        $comm_model = new Commission();
        $where[] = ["c.nickName", "like", "%" . $name . "%"];
        $where[] = ["e.agent_name", "like", "%" . $name . "%"];
        $where[] = ["f.coach_name", "like", "%" . $name . "%"];
        $where[] = ["h.user_name", "like", "%" . $name . "%"];
        $where[] = ["m.user_name", "like", "%" . $name . "%"];
        $where[] = ["i.user_name", "like", "%" . $name . "%"];
        $dis = [];
        $dis[] = ["b.status", ">", -1];
        if (!empty($type)) {
            if ($type == 2) {
                $dis[] = ["b.type", "in", [2, 5, 6]];
            } else {
                $dis[] = ["b.type", "=", $type];
            }
        }
        $change_order = [];
        if (in_array($type, [0, 3, 8])) {
            $where1[] = ["g.now_coach_name", "like", "%" . $name . "%"];
            $where1[] = ["b.type", "in", [3, 8]];
            $change_order = $this->alias("a")->join("massage_service_order_commission b", "a.id = b.order_id")->join("massage_order_coach_change_logs g", "if(a.is_add=0,a.id,a.add_pid) = g.order_id AND g.is_new = 1")->where($where1)->group("a.id")->column("a.id");
        }
        $order_id = $comm_model->alias("b")->join("massage_service_user_list c", "b.top_id = c.id  AND b.type in (1,14)", "left")->join("shequshop_school_admin e", "b.top_id = e.id AND b.type in (2,5,6,11,19,20)", "left")->join("massage_service_coach_list f", "b.top_id = f.id AND b.type in (3,8,17,18)", "left")->join("massage_channel_list h", "b.top_id = h.id AND b.type = 10", "left")->join("massage_salesman_list m", "b.top_id = m.id AND b.type = 12", "left")->join("massage_coach_broker_list i", "b.broker_id = i.id AND b.type = 9", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->group("b.id")->column("b.order_id");
        return array_merge($change_order, $order_id);
    }

    public function getFinanceObj($name, $type = 0)
    {
        $comm_model = new Commission();
        $dis = [];
        $dis[] = ["a.status", ">", -1];
        $dis[] = ["a.type", "<>", 11];
        if (!empty($type)) {
            if ($type == 2) {
                $dis[] = ["a.type", "in", [2, 5, 6]];
            } else {
                $dis[] = ["a.type", "=", $type];
            }
        }
        $dataPath = APP_PATH . "massage/info/FinanceObj.php";
        $arr = (include $dataPath);
        $list = [];
        foreach ($arr as $value) {
            $where = [];
            $table = $value["table"];
            $top_id = isset($value["field"]) ? $value["field"] : "b.top_id";
            $type = "(" . implode(",", $value["type"]) . ")";
            $user_name = $value["name"];
            $obj_type = $value["obj_type"];
            $where[] = ["c." . $user_name, "like", "%" . $name . "%"];
            $data = $comm_model->alias("a")->join($table . " c", $top_id . " = c.id  AND a.type in " . $type, "left")->where($dis)->where(function ($query) use ($where) {
                $query->whereOr($where);
            })->field("c." . $user_name . " as user_name,c.id as user_id,if(c.id=-1,0," . $obj_type . ") as obj_type")->group("c.id")->select()->toArray();
            $list = array_merge($list, $data);
        }
        return $list;
    }

    public function financeDetailedList($dis, $page = 10)
    {
        $data = $this->alias("b")->join("massage_service_order_commission a", "b.id = a.order_id", "left")->where($dis)->field("b.id as order_id,b.free_fare,b.pay_type,b.pay_price,b.true_car_price,b.coach_refund_time,b.coach_id,b.create_time as end_time,b.pay_model,b.balance,b.is_add,b.true_service_price,b.order_code,b.transaction_id,b.pay_price,b.car_price")->group("b.id")->order("b.id desc")->paginate($page)->toArray();
        $comm = new Commission();
        $share_model = new CommShare();
        $refund_model = new RefundOrder();
        $data = $comm->getFinanceText($data);
        if (!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["true_service_price"] = round($v["pay_price"], 2);
                $v["refund_cash"] = $refund_model->where(["order_id" => $v["order_id"], "status" => 2])->sum("refund_price");
                $v["refund_cash"] = round($v["refund_cash"], 2);
                $v = $share_model->financeShareData(["a.order_id" => $v["order_id"]], $v);
                $v["remain_cash"] = round($v["remain_cash"] - $v["poster_cash"] - $v["point_cash"] - $v["refund_cash"], 2);
                $car_admin = $comm->where(["order_id" => $v["order_id"], "type" => 13])->where("status", ">", -1)->find();
                $v["car_admin"] = !empty($car_admin) ? 1 : 0;
            }
        }
        return $data;
    }

    public function getTypeCommOrder($type)
    {
        $comm_model = new Commission();
        if ($type == 2) {
            $dis[] = ["type", "in", [2, 5, 6]];
        } elseif ($type == 8) {
            $dis[] = ["type", "in", [8, 13]];
        } else {
            $dis[] = ["type", "=", $type];
        }
        $dis[] = ["status", ">", -1];
        $order_id = $comm_model->where($dis)->column("order_id");
        return $order_id;
    }

    public function financeDetailedData($dis)
    {
        $data = $this->where($dis)->column("id");
        $dataPath = APP_PATH . "massage/info/FinanceText.php";
        $text = (include $dataPath);
        $comm = new Commission();
        $arr["true_service_price"] = $this->where($dis)->sum("true_service_price");
        $arr["true_car_price"] = $this->where($dis)->sum("true_car_price");
        $arr["remain_cash"] = round($arr["true_service_price"], 2);
        $arr["true_service_price"] = round($arr["true_service_price"] + $arr["true_car_price"], 2);
        $arr["true_car_price"] = round($arr["true_car_price"], 2);
        foreach ($text as $value) {
            $map = [];
            $map[] = ["order_id", "in", $data];
            $map[] = ["status", "=", 2];
            $map[] = ["type", "in", $value["type"]];
            if (!empty($value["city_type"])) {
                $map[] = ["city_type", "=", $value["city_type"]];
            }
            $cash = $comm->where($map)->sum("cash");
            $arr[$value["cash"]] = !empty($cash) ? round($cash, 2) : 0;
            if (!in_array(8, $value["type"])) {
                $arr["remain_cash"] -= $arr[$value["cash"]];
            }
        }
        $under_cash = $comm->where("order_id", "in", $data)->where("type", "in", [2, 5, 6])->where(["status" => 2])->sum("coach_cash");
        $arr["remain_cash"] = round($arr["remain_cash"] + $under_cash, 2);
        return $arr;
    }

    public function financeDetailedDataV2($where)
    {
        $dataPath = APP_PATH . "massage/info/FinanceTextAll.php";
        $text = (include $dataPath);
        $comm = new Commission();
        $share_model = new CommShare();
        $refund_model = new RefundOrder();
        $order = $this->alias("b")->join("massage_service_order_commission a", "a.order_id = b.id", "left")->where($where)->group("b.id")->column("b.pay_price,b.material_price");
        $arr["true_service_price"] = round(array_sum(array_column($order, "pay_price")), 2);
        $arr["remain_cash"] = $arr["true_service_price"];
        $arr["material_price"] = round(array_sum(array_column($order, "material_price")), 2);
        $cash_list = $comm->alias("c")->whereExists(function ($query) use ($where) {
            $query->name("massage_service_order_commission")->alias("a")->join("massage_service_order_list b", "a.order_id = b.id")->where(["a.status" => 2])->where($where)->whereRaw("c.order_id = b.id");
        })->where(["c.status" => 2])->group("c.type,c.city_type")->field("round(sum(c.coach_cash),2)as coach_cash,round(sum(c.car_cash),2)as car_cash,round(sum(c.cash),2) as cash,c.type,c.city_type")->select()->toArray();
        foreach ($text as $ks => $value) {
            $arr[$value["cash"]] = 0;
            $admin_share_cash = 0;
            if (!empty($cash_list)) {
                foreach ($cash_list as $v) {
                    if (in_array($v["type"], $value["type"]) && (empty($value["city_type"]) || $value["city_type"] == $v["city_type"])) {
                        $arr[$value["cash"]] += $v["cash"] - $v["coach_cash"] - $v["car_cash"];
                    }
                    if ($v["type"] == 23 && $ks == 0) {
                        $admin_share_cash = $v["cash"];
                    }
                }
            }
            $arr["remain_cash"] = $arr["remain_cash"] - $arr[$value["cash"]] + $admin_share_cash;
        }
        $arr["car_cash"] += $arr["admin_car_cash"];
        $car_point_cash = $share_model->alias("c")->join("massage_service_order_list b", "c.order_id = b.id")->join("massage_service_order_commission a", "a.order_id = b.id")->where($where)->where(["c.cash_type" => 1, "a.status" => 2])->where("c.comm_type", "in", [8, 13])->group("c.id")->column("c.share_cash");
        $car_point_cash = array_sum($car_point_cash);
        if (!empty($car_point_cash)) {
            $arr["car_cash"] .= "(手续费" . round($car_point_cash, 2) . ")";
        }
        $arr = $share_model->financeShareData($where, $arr, 2);
        $refund_cash = $refund_model->alias("c")->join("massage_service_order_list b", "c.order_id = b.id")->join("massage_service_order_commission a", "a.order_id = b.id")->where($where)->where(["c.status" => 2])->field("c.id")->group("c.id")->column("c.refund_price");
        $arr["refund_cash"] = round(array_sum($refund_cash), 2);
        $arr["remain_cash"] = round($arr["remain_cash"] - $arr["poster_cash"] - $arr["point_cash"] - $arr["refund_cash"], 2);
        return $arr;
    }

    public function financeDetailedDataV3($where)
    {
        $dataPath = APP_PATH . "massage/info/FinanceTextAll.php";
        $text = (include $dataPath);
        $comm = new Commission();
        $share_model = new CommShare();
        $refund_model = new RefundOrder();
        $order = $this->alias("b")->join("massage_service_order_commission a", "a.order_id = b.id", "left")->where($where)->group("b.id")->column("b.pay_price,b.material_price");
        $arr["true_service_price"] = round(array_sum(array_column($order, "pay_price")), 2);
        $arr["remain_cash"] = $arr["true_service_price"];
        $arr["material_price"] = round(array_sum(array_column($order, "material_price")), 2);
        $cash_list = $comm->alias("c")->whereExists(function ($query) use ($where) {
            $query->name("massage_service_order_commission")->alias("a")->join("massage_service_order_list b", "a.order_id = b.id")->where(["a.status" => 2])->where($where)->whereRaw("c.order_id = b.id");
        })->where(["c.status" => 2])->group("c.type,c.city_type")->field("round(sum(c.coach_cash),2)as coach_cash,round(sum(c.car_cash),2)as car_cash,round(sum(c.cash),2) as cash,c.type,c.city_type")->select()->toArray();
        foreach ($text as $ks => $value) {
            $arr[$value["cash"]] = 0;
            $admin_share_cash = 0;
            if (!empty($cash_list)) {
                foreach ($cash_list as $v) {
                    if (in_array($v["type"], $value["type"]) && (empty($value["city_type"]) || $value["city_type"] == $v["city_type"])) {
                        $arr[$value["cash"]] = $v["cash"] - $v["coach_cash"] - $v["car_cash"];
                    }
                    if ($v["type"] == 23 && $ks == 0) {
                        $admin_share_cash = $v["cash"];
                    }
                }
            }
            $arr["remain_cash"] = $arr["remain_cash"] - $arr[$value["cash"]] + $admin_share_cash;
        }
        $arr["car_cash"] += $arr["admin_car_cash"];
        $arr["coach_cash"] += $arr["coach_refund_empty_cash"] + $arr["coach_refund_comm_cash"];
        $arr["district_cash"] += $arr["district_refund_empty_cash"] + $arr["district_refund_comm_cash"];
        $arr["province_cash"] += $arr["province_refund_empty_cash"] + $arr["province_refund_comm_cash"];
        $arr["city_cash"] += $arr["city_refund_empty_cash"] + $arr["city_refund_comm_cash"];
        $car_point_cash = $share_model->alias("c")->join("massage_service_order_list b", "c.order_id = b.id")->join("massage_service_order_commission a", "a.order_id = b.id")->where($where)->where(["c.cash_type" => 1, "a.status" => 2])->where("c.comm_type", "in", [8, 13])->group("c.id")->column("c.share_cash");
        $car_point_cash = array_sum($car_point_cash);
        if (!empty($car_point_cash)) {
            $arr["car_cash"] .= "(手续费" . round($car_point_cash, 2) . ")";
        }
        $arr = $share_model->financeShareData($where, $arr, 2);
        $refund_cash = $refund_model->alias("c")->join("massage_service_order_list b", "c.order_id = b.id")->join("massage_service_order_commission a", "a.order_id = b.id")->where($where)->where(["c.status" => 2])->field("c.id")->group("c.id")->column("c.refund_price");
        $arr["refund_cash"] = round(array_sum($refund_cash), 2);
        $arr["remain_cash"] = round($arr["remain_cash"] - $arr["poster_cash"] - $arr["point_cash"] - $arr["refund_cash"], 2);
        return $arr;
    }

    public function financeDetailedSelect($dis)
    {
        $data = $this->alias("b")->join("massage_service_order_commission a", "b.id = a.order_id", "left")->where($dis)->field("b.id as order_id,b.free_fare,b.pay_type,b.pay_price,b.true_car_price,b.coach_refund_time,b.coach_id,b.create_time as end_time,b.pay_model,b.balance,b.is_add,b.true_service_price,b.order_code,b.transaction_id,b.pay_price,b.car_price")->group("b.id")->order("b.id desc")->select()->toArray();
        $comm = new Commission();
        $share_model = new CommShare();
        $refund_model = new RefundOrder();
        $arr["data"] = $data;
        $arr = $comm->getFinanceText($arr);
        if (!empty($arr["data"])) {
            foreach ($arr["data"] as &$v) {
                $v["true_service_price"] = round($v["pay_price"], 2);
                $v = $share_model->financeShareData(["a.order_id" => $v["order_id"]], $v);
                $v["refund_cash"] = $refund_model->where(["order_id" => $v["order_id"], "status" => 2])->sum("refund_price");
                $v["refund_cash"] = round($v["refund_cash"], 2);
                $v["remain_cash"] = round($v["remain_cash"] - $v["poster_cash"] - $v["point_cash"] - $v["refund_cash"], 2);
            }
        }
        return $arr["data"];
    }

    public function initCarprice($coach_id, $uniacid)
    {
        $comm_model = new Commission();
        $dis = ["a.coach_id" => $coach_id, "a.pay_type" => 7];
        $order_id = $this->alias("a")->join("massage_service_order_commission b", "a.id = b.order_id")->where($dis)->where("b.type", "in", [8, 13])->group("a.id")->column("a.id");
        $dis = ["coach_id" => $coach_id, "pay_type" => 7, "free_fare" => 0];
        $order = \think\facade\Db::name("massage_service_order_list")->where($dis)->where("true_car_price", ">", 0)->where("id", "not in", $order_id)->select()->toArray();
        $key = "initCarprice_key" . $coach_id;
        incCache($key, 1, $uniacid);
        $value = getCache($key, $uniacid);
        if ($value == 1 && !empty($order)) {
            foreach ($order as $value) {
                $comm_model->carCommission($value, 2);
            }
        }
        decCache($key, 1, $uniacid);
        return true;
    }

    public function getInitCoachInfo($order, $type = 1)
    {
        $change_log_model = new CoachChangeLog();
        $coach_model = new Coach();
        if (!empty($order["coach_id"])) {
            return $order;
        }
        if ($type == 1) {
            $add_id = $order["is_add"] == 0 ? $order["id"] : $order["add_pid"];
        } else {
            $add_id = $order["is_add"] == 0 ? $order["order_id"] : $this->where(["id" => $order["order_id"]])->value("add_pid");
        }
        $change_log = $change_log_model->dataInfo(["order_id" => $add_id, "status" => 1]);
        if (!empty($change_log)) {
            $order["coach_info"] = $coach_model->where(["id" => $change_log["init_coach_id"]])->field("id,uniacid,coach_name,mobile,work_img")->find();
        }
        return $order;
    }

    public function getAddOrderList($order_id, $is_coach = 0)
    {
        if ($is_coach == 0) {
            $data = $this->where(["add_pid" => $order_id])->where("pay_type", ">", 0)->field("id,over_time,order_code,pay_type,balance,material_price,pay_model")->order("id desc")->select()->toArray();
        } else {
            $data = $this->where(["add_pid" => $order_id])->where("pay_type", "not in", [-1, 1, 8])->field("id,over_time,order_code,pay_type,balance,material_price,pay_model")->order("id desc")->select()->toArray();
        }
        if (!empty($data)) {
            foreach ($data as &$vs) {
                $vs["over_time"] -= time();
                $vs["over_time"] = 0 < $vs["over_time"] ? $vs["over_time"] : 0;
                if (!empty($vs["order_goods"]) && $is_coach == 0) {
                    foreach ($vs["order_goods"] as &$v) {
                        $v["price"] = round($v["price"] + $v["init_material_price"], 2);
                        $v["true_price"] = round($v["true_price"] + $v["material_price"], 2);
                    }
                }
                $can_refund_num = array_sum(array_column($vs["order_goods"], "can_refund_num"));
                if (in_array($vs["pay_type"], [2, 3, 4, 5]) && 0 < $can_refund_num) {
                    $vs["can_refund"] = 1;
                } else {
                    $vs["can_refund"] = 0;
                }
            }
        }
        return $data;
    }

    public function adminOrderCanRefund($order_id)
    {
        $order = $this->dataInfo(["id" => $order_id]);
        $refund_model = new RefundOrder();
        if (0 < $order["true_service_price"] || 0 < $order["material_price"]) {
            return 1;
        }
        $can_refund_car_price = $refund_model->canRefundOrderPrice($order["id"]);
        if (0 < $can_refund_car_price) {
            return 1;
        }
        return 0;
    }

    public function orderCanRefund($order_id)
    {
        $order = $this->dataInfo(["id" => $order_id]);
        $can_refund_num = is_array($order["order_goods"]) ? array_sum(array_column($order["order_goods"], "can_refund_num")) : 0;
        if (in_array($order["pay_type"], [2, 3, 4, 5, 6]) && 0 < $can_refund_num) {
            return 1;
        }
        return 0;
    }

    public function hxAddOrder($order, $coach_id = 0, $is_admin = 0, $control_id = 0)
    {
        $refund_model = new RefundOrder();
        $log_model = new OrderLog();
        if ($order["is_add"] == 0) {
            $add_order = $this->where(["add_pid" => $order["id"], "pay_type" => 2])->find();
            if (!empty($add_order)) {
                return ["code" => 500, "msg" => "该订单还有待接单待加钟订单，请先处理"];
            }
            $add_order = $this->where(["add_pid" => $order["id"], "pay_type" => 1])->find();
            if (!empty($add_order)) {
                return ["code" => 500, "msg" => "该订单还有未支付的加钟订单，请先联系客户支付或者取消"];
            }
            $add_order = $this->where(["add_pid" => $order["id"]])->where("pay_type", "in", [3, 4, 5, 6])->select()->toArray();
            if (!empty($add_order)) {
                foreach ($add_order as $value) {
                    $refund_order = $refund_model->where(["order_id" => $value["id"]])->where("status", "in", [1, 4, 5])->count();
                    if (0 < $refund_order) {
                        return ["code" => 500, "msg" => "该订单加钟订单正在申请退款，请先联系平台处理再进行下一步"];
                    }
                    $res = $this->hxOrder($value, $coach_id);
                    if (!empty($res["code"])) {
                        return $res;
                    }
                    $log_model->addLog($value["id"], $value["uniacid"], 7, $value["pay_type"], $is_admin, $control_id);
                }
            }
        }
        return true;
    }

    public function hxOrder($order, $cap_id = 0)
    {
        $time = time();
        $update = ["order_end_time" => $time, "pay_type" => 7, "hx_user" => $cap_id, "can_tx_date" => $time];
        \think\facade\Db::startTrans();
        $res = $this->dataUpdate(["id" => $order["id"]], $update);
        $called = new \app\virtual\model\Config();
        $called->delBindVirtualPhone($order);
        $res = $this->coachBalanceArr($order["uniacid"], $order["id"]);
        if (!empty($res["code"])) {
            \think\facade\Db::rollback();
            return $res;
        }
        \think\facade\Db::commit();
        $level_model = new \app\member\model\Level();
        $level_model->levelUp($order);
        $coach_model = new Coach();
        $coach_info = $coach_model->dataInfo(["id" => $order["coach_id"]]);
        if (!empty($coach_info)) {
            $coach_model->where(["id" => $coach_info["id"]])->update(["total_order_num" => \think\facade\Db::Raw("total_order_num+1")]);
        }
        $credit_model = new CreditConfig();
        if ($order["is_add"] == 0) {
            $find = $this->where(["coach_id" => $order["coach_id"], "user_id" => $order["user_id"], "pay_type" => 7])->where("id", "<>", $order["id"])->find();
            if (!empty($find)) {
                $type = 4;
            } else {
                $type = 1;
            }
        } else {
            $type = 2;
        }
        $credit_model->creditRecordAdd($order["coach_id"], $type, $order["uniacid"], $order["id"], $order["true_service_price"], $order["create_time"]);
        $credit_model->creditRecordAdd($order["coach_id"], 3, $order["uniacid"], $order["id"], $order["true_time_long"], $order["create_time"]);
        return $res;
    }

    public function coachBalanceArr($uniacid, $order_id = 0)
    {
        $key = "key_coach_arr_aaaann11aa" . $order_id;
        incCache($key, 1, $uniacid, 30);
        $key_value = getCache($key, $uniacid);
        if ($key_value == 1) {
            $dis[] = ["uniacid", "=", $uniacid];
            $dis[] = ["pay_type", "=", 7];
            $dis[] = ["have_tx", "=", 0];
            if (!empty($order_id)) {
                $dis[] = ["id", "=", $order_id];
            }
            $order = $this->where($dis)->field("admin_id,admin_cash,id as order_id,coach_id,service_price,car_price,true_service_price,true_car_price,admin_cash,coach_cash")->select()->toArray();
            if (!empty($order)) {
                $refund_model = new RefundOrder();
                $comm_model = new Commission();
                foreach ($order as $value) {
                    $value["id"] = $value["order_id"];
                    $refund_order = $refund_model->dataInfo(["order_id" => $value["id"], "status" => 1]);
                    if (empty($refund_order)) {
                        \think\facade\Db::startTrans();
                        try {
                            $res = $this->where(["id" => $value["id"], "have_tx" => 0])->update(["have_tx" => 1]);
                            if ($res != 0) {
                                $res = $comm_model->commissionSucessCash($value["id"]);
                                if ($res == 0) {
                                    \think\facade\Db::rollback();
                                    if (!empty($order_id)) {
                                        decCache($key, 1, $uniacid);
                                        return ["code" => 500, "msg" => "网络不佳,核销失败,请重试"];
                                    }
                                }
                            }
                        } catch (\Exception $e) {
                            \think\facade\Db::rollback();
                        }
                        \think\facade\Db::commit();
                    }
                }
            }
        }
        decCache($key, 1, $uniacid);
        return true;
    }

    public function checkOrderStatus($order_type, $input_type, $is_add)
    {
        if ($order_type < 2) {
            return ["code" => 500, "msg" => "订单已被取消,请刷新页面"];
        }
        if ($order_type == 7) {
            return ["code" => 500, "msg" => "订单已经完成,请刷新页面"];
        }
        if ($order_type == $input_type) {
            return ["code" => 500, "msg" => "订单状态错误，请刷新"];
        }
        if ($input_type == -1 && !in_array($order_type, [2, 8])) {
            return ["code" => 500, "msg" => "订单状态错误，请刷新"];
        }
        if ($input_type == 3 && $order_type != 2) {
            return ["code" => 500, "msg" => "订单状态错误，请刷新"];
        }
        if ($input_type == 4 && $order_type != 3) {
            return ["code" => 500, "msg" => "订单状态错误，请刷新"];
        }
        if ($input_type == 5 && $order_type != 4) {
            return ["code" => 500, "msg" => "订单状态错误，请刷新"];
        }
        if ($input_type == 6 && !in_array($order_type, [2, 3, 5])) {
            return ["code" => 500, "msg" => "订单状态错误，请刷新"];
        }
        if ($input_type == 7 && $order_type != 6) {
            return ["code" => 500, "msg" => "订单状态错误，请刷新"];
        }
        return true;
    }

    public function useOrderTimes($user_id, $order_create_time)
    {
        $times = $this->where(["user_id" => $user_id])->where("pay_time", ">", 0)->where("create_time", "<=", $order_create_time)->count();
        return $times;
    }

    public function initOrderData($uniacid)
    {
        $mapor[] = ["init_material_price", ">", 0];
        $mapor[] = ["material_price", ">", 0];
        $order = $this->where(["uniacid" => $uniacid])->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->where("pay_time", ">", 0)->where("start_material_price", "<", 0)->field("id as order_id,discount,init_service_price,service_price,init_material_price,material_price,car_price,pay_price,free_fare")->select()->toArray();
        lbGetfDates();
        if (!empty($order)) {
            foreach ($order as $key => $value) {
                $value["car_price"] = $value["free_fare"] == 0 ? $value["car_price"] : 0;
                $start_material_price = round($value["pay_price"] - $value["car_price"] - $value["service_price"], 2);
                $update[$key] = ["id" => $value["order_id"], "start_material_price" => $start_material_price];
            }
            $this->saveAll($update);
        }
        return true;
    }

    public function initCoachRefundOrder($uniacid)
    {
        $key = "initCoachRefundOrder";
        incCache($key, 1, $uniacid);
        if (getCache($key, $uniacid) == 1) {
            \think\facade\Db::startTrans();
            $refund_model = new RefundOrder();
            $refund_model->where(["uniacid" => $uniacid, "type" => 2, "init" => 0, "status" => 2])->update(["status" => -1]);
            $map[] = ["coach_refund_time", ">", 0];
            $map[] = ["coach_refund_code", ">", 0];
            $order_id = $refund_model->where(["uniacid" => $uniacid, "type" => 2, "status" => 2])->column("order_id");
            $data = $this->where(["pay_type" => -1])->where("id", "not in", $order_id)->where("pay_time", ">", 0)->where(function ($query) use ($map) {
                $query->whereOr($map);
            })->field("id as order_id,uniacid,time_long,pay_price,coach_id,user_id,admin_id,partner_id,start_material_price,service_price,car_price,free_fare,is_add,balance,coach_refund_time,create_time,coach_refund_text")->select()->toArray();
            if (!empty($data)) {
                foreach ($data as $k => $v) {
                    $insert[$k] = ["uniacid" => $v["uniacid"], "user_id" => $v["user_id"], "admin_id" => $v["admin_id"], "partner_id" => $v["partner_id"], "time_long" => $v["time_long"], "order_code" => orderCode(), "coach_id" => $v["coach_id"], "apply_price" => $v["pay_price"], "refund_price" => $v["pay_price"], "have_price" => $v["pay_price"], "service_price" => $v["service_price"], "material_price" => 0 <= $v["start_material_price"] ? $v["start_material_price"] : 0, "refund_service_price" => $v["service_price"], "refund_material_price" => 0 <= $v["start_material_price"] ? $v["start_material_price"] : 0, "order_id" => $v["order_id"], "is_add" => $v["is_add"], "car_price" => $v["free_fare"] == 0 ? $v["car_price"] : 0, "refund_car_price" => $v["free_fare"] == 0 ? $v["car_price"] : 0, "balance" => !empty($v["balance"]) ? $v["pay_price"] : 0, "type" => 2, "status" => 2, "refund_time" => $v["coach_refund_time"], "create_time" => $v["create_time"], "refund_text" => $v["coach_refund_text"], "version" => 2, "init" => 1];
                }
                $refund_model->saveAll($insert);
            }
            \think\facade\Db::commit();
        }
        decCache($key, 1, $uniacid);
        return true;
    }

    public function channelDataList($dis, $mapor, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_order_goods_list b", "a.id = b.order_id")->join("massage_service_order_commission c", "a.id = c.order_id")->where($dis)->where("c.type", "=", 10)->where("c.status", ">", -1)->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->field("c.cash as channel_cash,c.id as comm_id,a.pay_model,a.balance,a.label_time,a.have_tx,a.id,a.coach_id,a.store_id,a.is_comment,a.order_code,a.true_service_price,a.pay_type,a.pay_price,a.start_time,a.create_time,a.user_id,a.end_time,a.add_pid,a.is_add,a.init_material_price,a.material_price")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        if (!empty($data["data"])) {
            $abn_model = new \app\abnormalorder\model\OrderList();
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                $v["start_time"] = date("Y-m-d H:i", $v["start_time"]);
                $v["end_time"] = date("Y-m-d H:i", $v["end_time"]);
                $v["abn_order_id"] = $abn_model->where(["order_id" => $v["id"]])->value("id");
            }
        }
        return $data;
    }

    public function brokerDataList($dis, $mapor, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_order_goods_list b", "a.id = b.order_id")->join("massage_service_order_commission c", "a.id = c.order_id")->where($dis)->where("c.type", "=", 9)->where("c.status", ">", -1)->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->field("c.cash as broker_cash,c.balance as broker_balance,c.id as comm_id,a.pay_model,a.balance,a.label_time,a.have_tx,a.id,a.coach_id,a.store_id,a.is_comment,a.order_code,a.true_service_price,a.pay_type,a.pay_price,a.start_time,a.create_time,a.user_id,a.end_time,a.add_pid,a.is_add,a.init_material_price,a.material_price")->group("a.id")->order("a.id desc")->paginate($page)->toArray();
        if (!empty($data["data"])) {
            $abn_model = new \app\abnormalorder\model\OrderList();
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                $v["start_time"] = date("Y-m-d H:i", $v["start_time"]);
                $v["end_time"] = date("Y-m-d H:i", $v["end_time"]);
                $v["abn_order_id"] = $abn_model->where(["order_id" => $v["id"]])->value("id");
            }
        }
        return $data;
    }

    public function canRefundOrder($data, $after_service_can_refund, $max_minute)
    {
        $can_refund_num = array_sum(array_column($data["order_goods"], "can_refund_num"));
        $refund_type = $after_service_can_refund == 1 ? [2, 3, 4, 5, 6] : [2, 3, 4, 5];
        if (in_array($data["pay_type"], $refund_type) && 0 < $can_refund_num) {
            $res = true;
            if ($data["pay_type"] == 6) {
                $service_time = time() - $data["start_service_time"];
                if ($max_minute * 60 < $service_time) {
                    $res = false;
                }
            }
        } else {
            $res = false;
        }
        return $res;
    }
}

?>